package nku.csc360;

//Raj Akula
import java.util.*;
import java.io.*;

public class Problem2 {

	public static void main (String... Args)throws Exception{
		int lowestValid = 0;
		int highestValid = 0;
		
		InputStreamReader input = new InputStreamReader(System.in);
		BufferedReader reader = new BufferedReader(input);
		LinkedList<String> link=new LinkedList<String>();
		int index;
		String choice = "start";
		String string;
		while(!choice.equals("quit"))
		{	
				System.out.println("Current long List: " + link);
				System.out.print("Enter command[add|set|remove|quit]: ");
				choice = reader.readLine();

				if(choice.equals("add")){
					index = promptForIndex(reader, lowestValid, highestValid);
					System.out.print("Enter a string: ");
					string = reader.readLine();
					link.add(index,string);
					highestValid++;
				}
				else if (choice.equals("set")){
					if(highestValid < 1)
					{
						System.out.println("No elements in list to set!!! Try Again!!!");
						continue;
					}
					index = promptForIndex(reader, lowestValid, highestValid-1);
					System.out.print("Enter a string: ");
					string = reader.readLine();
					link.set(index,string);
				}
				else if (choice.equals("remove")){
					if(highestValid < 1)
					{
						System.out.println("No elements in list to set!!! Try Again!!!");
						continue;
					}
					index = promptForIndex(reader, lowestValid, highestValid-1);
					link.remove(index);
					highestValid--;
				}
				else if (choice.equals("quit"))
					return;
				else{
					System.out.println("Unrecognized Command : " + choice);
					System.out.print("Enter command[add|set|remove|quit]: ");
					choice = reader.readLine();
				}
				
			
		}	
		
	}
    
    @SuppressWarnings("finally")
	public static int promptForIndex (BufferedReader reader, int lowestValid, int highestValid){
    	int index=0;
    	System.out.print("Enter a Index: ");
    	try{
    		index = Integer.parseInt(reader.readLine());
    		if(index < lowestValid || index > highestValid){
    			System.out.println("Try Again Valid indexes are (" + lowestValid +"..." + highestValid + ")!!!!");
    			index = promptForIndex(reader, lowestValid, highestValid);
    		}
    	}
    	catch (Exception ex) {
      		System.out.println("Not a number!!! Try Again!!!");
      		index = promptForIndex(reader, lowestValid, highestValid);
    	}
    	finally{
    		return index;
    	}
    	
    	
    }
}